package com.lgkj.ui;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.lgkj.R;
import com.lgkj.adapter.MovieAdapter;
import com.lgkj.adapter.MovieTypeAdapter;
import com.lgkj.bean.MovieBean;
import com.lgkj.bean.MovieTypeBean;
import com.lgkj.utils.CommonConstant;
import com.lgkj.utils.HttpUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/7/9 0009.
 */

public class MovieActivity extends BaseActivity {
    @BindView(R.id.rc_title)
    RecyclerView rcTitle;
    @BindView(R.id.lv_movie)
    ListView lvMovie;
    private MovieAdapter adapter;
    private MovieBean movieBean;
    private MovieTypeBean movieTypeBean;
    private MovieTypeAdapter typeAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_movie;
    }

    @Override
    public void initView() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MovieActivity.this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        rcTitle.setLayoutManager(linearLayoutManager);
        typeAdapter = new MovieTypeAdapter(MovieActivity.this);
        rcTitle.setAdapter(typeAdapter);

        adapter = new MovieAdapter(MovieActivity.this);
        lvMovie.setAdapter(adapter);
        setTitle("电影列表");
        ItemClick();
    }

    @Override
    public void initData() {
        showDiaLog("正在获取数据");
        Map<String, String> map = new HashMap<>();
        map.put("news_type", "1");
        HttpUtils.postHttp(map, CommonConstant.videoType, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                dismissDiaLog();
                if (jsonObject.optInt("status") == 1) {
                    movieTypeBean = new Gson().fromJson(String.valueOf(jsonObject), MovieTypeBean.class);
                    if (movieTypeBean != null && movieTypeBean.getData() != null) {
                        typeAdapter.setdata(movieTypeBean.getData());
                        typeAdapter.notifyDataSetChanged();
                        if (movieTypeBean.getData() != null && movieTypeBean.getData().get(0).getType() != null) {
                            String type_id = movieTypeBean.getData().get(0).getType().getVideo_type_id();
                            initDatas(type_id);
                        }
                    }
                }
            }

            @Override
            public void errorHttp(String msg) {
                dismissDiaLog();
            }
        });

    }

    private void initDatas(String id) {
        showDiaLog("正在获取数据");
        Map<String, String> map = new HashMap<>();
        map.put("video_type_id", id);
        HttpUtils.postHttp(map, CommonConstant.videoList, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                dismissDiaLog();
                if (jsonObject.optInt("status") == 1) {
                    movieBean = new Gson().fromJson(String.valueOf(jsonObject), MovieBean.class);
                    if (movieBean != null && movieBean.getData() != null) {
                        adapter.setData(movieBean.getData());
                        adapter.notifyDataSetChanged();
                    }
                }
            }

            @Override
            public void errorHttp(String msg) {
                dismissDiaLog();
            }
        });
    }


    private void ItemClick() {
        lvMovie.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(MovieActivity.this, MovieDetailsActivity.class);
                intent.putExtra("video_id", adapter.getItem(i).getVideo_id());
                startActivity(intent);
            }
        });
        typeAdapter.setItemClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int id = Integer.parseInt(view.getTag().toString());
                initDatas(typeAdapter.getItem(id).getType().getVideo_type_id());
            }
        });
    }

}
